package com.dtjd.playcoinmonkey.activities;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.dtjd.playcoinmonkey.MyApplication;
import com.dtjd.playcoinmonkey.R;
import com.dtjd.playcoinmonkey.bean.HttpParaBean;
import com.dtjd.playcoinmonkey.databinding.ActivityTipoffBinding;
import com.dtjd.playcoinmonkey.utils.Network;
import com.dtjd.playcoinmonkey.utils.Tools;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class TipoffActivity extends AppCompatActivity {
    private final List<String> types=new ArrayList<>();
    private boolean type_Discrimination=false,
            type_False=false,
            type_Swindle=false,
            type_Tort=false,
            type_Other=false,
            type_Violent=false,
            type_Gambling=false;
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){ finish(); }
        return super.onOptionsItemSelected(item);
    }
    private ActivityTipoffBinding binding;
    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= ActivityTipoffBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setTitle(getResources().getString(R.string.tip_off));
        ActionBar actionbar = getSupportActionBar();
        if (actionbar!=null){
            actionbar.setDisplayHomeAsUpEnabled(true);
            actionbar.setHomeButtonEnabled(true);
        }
        binding.tipoffTypeDiscrimination.setOnClickListener(view -> {
            if (type_Discrimination){
                type_Discrimination=false;
                binding.tipoffTypeDiscrimination.setBackground(getResources().getDrawable(R.drawable.bg_button2));
            }else {
                type_Discrimination=true;
                binding.tipoffTypeDiscrimination.setBackground(getResources().getDrawable(R.drawable.bg_button2_grey));
            }
        });
        binding.tipoffTypeFalse.setOnClickListener(view -> {
            if (type_False){
                type_False=false;
                binding.tipoffTypeFalse.setBackground(getResources().getDrawable(R.drawable.bg_button2));
            }else {
                type_False=true;
                binding.tipoffTypeFalse.setBackground(getResources().getDrawable(R.drawable.bg_button2_grey));
            }
        });
        binding.tipoffTypeGambling.setOnClickListener(view -> {
            if (type_Gambling){
                type_Gambling=false;
                binding.tipoffTypeGambling.setBackground(getResources().getDrawable(R.drawable.bg_button2));
            }else {
                type_Gambling=true;
                binding.tipoffTypeGambling.setBackground(getResources().getDrawable(R.drawable.bg_button2_grey));
            }
        });
        binding.tipoffTypeSwindle.setOnClickListener(view -> {
            if (type_Swindle){
                type_Swindle=false;
                binding.tipoffTypeSwindle.setBackground(getResources().getDrawable(R.drawable.bg_button2));
            }else {
                type_Swindle=true;
                binding.tipoffTypeSwindle.setBackground(getResources().getDrawable(R.drawable.bg_button2_grey));
            }
        });
        binding.tipoffTypeVilify.setOnClickListener(view -> {
            if (type_Swindle){
                type_Swindle=false;
                binding.tipoffTypeVilify.setBackground(getResources().getDrawable(R.drawable.bg_button2));
            }else {
                type_Swindle=true;
                binding.tipoffTypeVilify.setBackground(getResources().getDrawable(R.drawable.bg_button2_grey));
            }
        });
        binding.tipoffTypeTort.setOnClickListener(view -> {
            if (type_Tort){
                type_Tort=false;
                binding.tipoffTypeTort.setBackground(getResources().getDrawable(R.drawable.bg_button2));
            }else {
                type_Tort=true;
                binding.tipoffTypeTort.setBackground(getResources().getDrawable(R.drawable.bg_button2_grey));
            }
        });
        binding.tipoffTypeViolent.setOnClickListener(view -> {
            if (type_Violent){
                type_Violent=false;
                binding.tipoffTypeViolent.setBackground(getResources().getDrawable(R.drawable.bg_button2));
            }else {
                type_Violent=true;
                binding.tipoffTypeViolent.setBackground(getResources().getDrawable(R.drawable.bg_button2_grey));
            }
        });
        binding.tipoffTypeOther.setOnClickListener(view -> {
            if (type_Other){
                type_Other=false;
                binding.tipoffTypeOther.setBackground(getResources().getDrawable(R.drawable.bg_button2));
            }else {
                type_Other=true;
                binding.tipoffTypeOther.setBackground(getResources().getDrawable(R.drawable.bg_button2_grey));
            }
        });
        binding.tipoffSubmit.setOnClickListener(view -> {
            if (!binding.tipoffContent.getText().toString().equals("")){
                List<String>list=new ArrayList<>();
                if (type_False)list.add("False");
                if (type_Gambling)list.add("Gambling");
                if (type_Violent)list.add("Violent");
                if (type_Swindle)list.add("Swindle");
                if (type_Other)list.add("Other");
                if (type_Discrimination)list.add("Discrimination");
                if (type_Tort)list.add("Tort");
                if (list.size()>0){
                    StringBuilder type= new StringBuilder();
                    for (int i = 0; i < list.size(); i++)
                        if (i==0){
                            type.append(list.get(i));
                        }else {
                            type.append(",").append(list.get(i));
                        }
                    List<HttpParaBean> para=new ArrayList<>();
                    para.add(new HttpParaBean("token","token"));
                    para.add(new HttpParaBean("wrType",type.toString()));
                    para.add(new HttpParaBean("id", MyApplication.postBean.id));
                    para.add(new HttpParaBean("wrReason",binding.tipoffContent.getText().toString()));
                    Network.post(para, "wbhMain/tipOffPost", new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                            e.printStackTrace();
                        }
                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response){
                            runOnUiThread(() -> {
                                try {
                                    JSONObject jo = new JSONObject(Objects.requireNonNull(response.body()).string());
                                    Tools.toast( jo.getString("datalist"));
                                    if (jo.getInt("code")==2){
                                        TipoffActivity.this.finish();
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            });
                        }
                    });
                }else {
                    Tools.toast( getResources().getString(R.string.tip_off_label_tip));
                }
            }else {
                Tools.toast( getResources().getString(R.string.content_cant_blank));
            }
        });
    }
}